package com.youdoneed.news.news.service;

import com.youdoneed.base.application.AppException;
import com.youdoneed.news.news.dao.CmfAuthorFocusMapper;
import com.youdoneed.news.news.dao.CmfAuthorRelationMapper;
import com.youdoneed.news.news.dao.CmfAuthorsMapper;
import com.youdoneed.news.news.entity.CmfAuthorFocus;
import com.youdoneed.news.news.entity.CmfAuthors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CmfAuthorService {

    private final Logger logger = LoggerFactory.getLogger(CmfAuthorService.class);

    @Autowired
    private CmfAuthorFocusMapper cmfAuthorFocusMapper;
    @Autowired
    private CmfAuthorsMapper cmfAuthorsMapper;

    /**
     * 添加关注新闻号
     *
     * @param authorId
     * @param userId
     * @return
     * @author hexz
     * @date 2017.7.3
     */
    @Transactional
    public int addAuthorsFocus(Integer authorId, String userId) {
        CmfAuthorFocus isFocus = cmfAuthorFocusMapper.checkIsFocus(authorId, userId);
        if (null != isFocus) {
            throw new AppException("用户已经关注该新闻号");
        }
        CmfAuthorFocus authorFocus = new CmfAuthorFocus();
        String time = String.valueOf(System.currentTimeMillis() / 1000);
        authorFocus.setAuthorId(authorId);
        authorFocus.setFaceUid(userId);
        authorFocus.setAddTime(Integer.valueOf(time));
        return cmfAuthorFocusMapper.insert(authorFocus);
    }

    /**
     * 取消关注新闻号
     *
     * @param authorId
     * @param userId
     * @return
     * @author hexz
     * @date 2017.7.3
     */
    @Transactional
    public int cancelAuthorsFocus(Integer authorId, String userId) {
        CmfAuthorFocus isFocus = cmfAuthorFocusMapper.checkIsFocus(authorId, userId);
        //用户未关注新闻号
        if (null == isFocus) {
            throw new AppException("用户未关注新闻号");
        }
        return cmfAuthorFocusMapper.deleteByPrimaryKey(isFocus.getId());
    }

    /**
     * 获取新闻与来源对应关系
     *
     * @return
     */
    public CmfAuthors getAuthorsOfPosts(Integer authorId) {
        CmfAuthors authors = cmfAuthorsMapper.getPostsAuthors(authorId);
        return authors;
    }

    /**
     * 获取当前用户关注新闻源
     *
     * @param userId
     * @return
     */
    public List<CmfAuthors> getFocusAuthors(String userId) {
        List<CmfAuthorFocus> authorFocus = cmfAuthorFocusMapper.getFocusNewsAuthor(userId);
        if (null == authorFocus || authorFocus.size() < 1) {
            return null;
        }
        List<CmfAuthors> authors = cmfAuthorsMapper.getFocusAuthors(authorFocus);
        return authors;
    }

    /**
     * 更新新闻作者被关注数
     *
     * @param authorId
     * @return
     */
    public int updateFocusCount(Integer authorId, String focusType) {
        CmfAuthors authors = cmfAuthorsMapper.selectByPrimaryKey(authorId);
        CmfAuthors upAuthors = new CmfAuthors();
        if (null != authors) {
            Integer focusCount = authors.getFocusCount();
            focusCount = (null == focusCount) ? 0 : focusCount;
            if ("0".equals(focusType)) {
                upAuthors.setId(authorId);
                upAuthors.setFocusCount(focusCount + 1);
            } else if ("1".equals(focusType)) {
                upAuthors.setId(authorId);
                upAuthors.setFocusCount(focusCount - 1);
            }
        }
        return cmfAuthorsMapper.updateByPrimaryKeySelective(upAuthors);
    }

}
